﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YXQ.Common.Helper;
using YXQ.Dao.ViewModel;
using YXQ.Service;

namespace YXQ.Manager.Manager
{
    public partial class UserRight : BaseAdminPage
    {

        public UMModel ummd { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                hfid.Value = reqID.ToString();
                GetUser();
                GetDepart();
            }
        }

        private void GetDepart()
        {
            StringBuilder sb = new StringBuilder();

            List<DepartModel> departs = CenterService.departService.GetAllDepart();
            foreach (var dp in departs)
            {
                string chk = string.Empty;
                if ( ummd!=null)
                {
                    if (!string.IsNullOrEmpty(ummd.DepartId))
                    {
                        if (ummd.DepartId.Contains(dp.DepartNum))
                        {
                            chk = "checked=checked";
                        }
                    }
                }
                sb.AppendFormat("<input type='checkbox' name='chkdp' value='{0}' {2}>{1}<br/>", dp.DepartNum, dp.DepartName, chk);
            }
            ltrDepart.Text = sb.ToString();
        }

        private void GetUser()
        {
            UserModel umd = CenterService.userService.GetOneById(reqID);
            ummd = CenterService.uMService.GetOneByUId(reqID);
            if (umd != null)
            {
                ltrUName.Text = umd.UserName;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var depart = Request["chkdp"];
            UMModel umd = new UMModel();
            umd.Id = Convert.ToInt32(hfid.Value);
            umd.UserId = reqID;
            umd.MudleId = hfRight.Value;
            umd.DepartId = Request["chkdp"];
            if (CenterService.uMService.Save(umd) > 0)
            {
                JsHelper.AlertAndClose(this, "数据保存成功！");
            }
            else {
                JsHelper.AlertAndClose(this, "数据保存失败！");
            }
        }
    }
}